package com.tanhua.api.db;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tanhua.db.User;
import com.tanhua.db.UserInfo;
import com.tanhua.mapper.UserInfoMapper;
import com.tanhua.mapper.UserMapper;
import com.tanhua.vo.UserHolder;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

@Service
public class UserApiImpl implements UserApi {

    @Autowired
    private UserMapper userMapper;


    @Autowired
    private UserInfoMapper userInfoMapper;


    @Override
    public Long saveUser(User user) {
        userMapper.insert(user);
        return user.getId(); //主键返回策略  mybatisplus自动开启了主键返回
    }

    @Override
    public User findByPhone(String phone) {
        QueryWrapper<User> qw = new QueryWrapper<>(); //条件
        qw.eq("phone",phone);
        User user = userMapper.selectOne(qw);
        return user;
    }


    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public void updateUser(String phone1,String phone2) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone1);
        User user = new User();
        user.setPhone(phone2);
        userMapper.update(user,queryWrapper);
    }
}
